<?php

require_once '../application/modules/admin/controllers/SuperControllerAdmin.php';

class Admin_CategoriesController extends Admin_SuperController {

    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
            if ($this->_request->event == 'removeRow')
                $this->onRemoweRow($this->_request->id);
            if ($this->_request->event == 'updateCat')
                $this->onUpdateCat($this->getRequest()->getParams());
            if ($this->_request->event == 'createCat')
                $this->onCreateCat($this->getRequest()->getParams());
        }

        //_debug($this->getRequest()->getParams());
        $this->view->order_field = isset($this->_request->order_field) ? $this->_request->order_field : 'order_by';
        $this->view->order_dir = isset($this->_request->order_dir) ? $this->_request->order_dir : 'ASC';

        $this->view->tableColumn = array(
            0 => array(
                'title' => 'title',
            ),
            1 => array(
                'title' => 'url',
            ),
            2 => array(
                'title' => 'description',
                'wrap' => 'div150'
            ),
            3 => array(
                'title' => 'metakeywords',
                'wrap' => 'div150'
            ),
            5 => array(
                'title' => 'order_by',
            ),
            6 => array(
                'title' => 'active',
            ),
            7 => array(
                'title' => 'left_menu',
            ),
            8 => array(
                'title' => 'Change',
            ),
        );

        $CATM = new Admin_Model_Cats();
        $this->view->Info = $CATM->getCats($this->_request->title_cat);
    }

    protected $sructure = array(
        0 => array('name' => 'title', 'type' => 'str', 'valid' => true,),
        1 => array('name' => 'url', 'type' => 'str'),
        2 => array('name' => 'parent_id', 'type' => 'build', 'function' => 'getCategoriesSelect', 'value' => ''),
        3 => array('name' => 'description', 'type' => 'text'),
        4 => array('name' => 'metakeywords', 'type' => 'text'),
        5 => array('name' => 'active', 'value'=>1, 'type' => 'one-checkbox',),
        6 => array('name' => 'left_menu', 'value'=>1, 'type' => 'one-checkbox',),
        7 => array('name' => 'order_by', 'type' => 'str', 'valid' => true, 'class' => ' thisInt', 'style' => 'width: 50px;'),
    );

    public function createCategoriesAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $this->info = array();
        $this->info['id'] = 0;
        $this->info['event'] = 'createCat';
        $this->info['fields'] = $this->sructure;
        include (APPLICATION_PATH . '/modules/admin/views/scripts/snippets/lego.phtml');
    }

    public function editCategoriesAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $cat_id = (int) $this->_request->id;
        if (!empty($cat_id)) {
            $CONTM = new Admin_Model_Content();
            $info = $CONTM->getOneObj('categories', $cat_id);
            if (is_array($info)) {
                $this->info = array();
                $this->info['id'] = $info['id'];
                $this->info['event'] = 'updateCat';
                $this->info['fields'] = $this->addValue($this->sructure, $info);
                $this->info['fields'][2]['value'] = $info['parent_id'];
                include (APPLICATION_PATH . '/modules/admin/views/scripts/snippets/lego.phtml');
            }
        } else {
            throw new Exception('no parameters required');
        }
    }

    protected function onRemoweRow($id) {
        if (!empty($id)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->removeRow('categories', $id);
        }
    }

    protected function onUpdateCat($data) {
        if (!empty($data)) {
            $ACNTM = new Admin_Model_Content();
            $data['parent_id'] = (int) $data['category_id'];
            $parent = $ACNTM->getOne('SELECT `parent_id` FROM `categories` WHERE `id` = '.$data['parent_id']);
            if($data['parent_id'] == $data['id'] || !empty ($parent)){
                MessBox::setMessage('no_correct_parrent');
                return;
            }
            if(empty ($data['url']))
                $data['url'] = strtolower (_translit (trim($data['title'])));
            if(!isset ($data['active']))
                $data['active'] = 0;
            if(!isset ($data['left_menu']))
                $data['left_menu'] = 0;
            unset($data['category_id']);
            $ACNTM->update('categories', $data);
        }
    }

    protected function onCreateCat($data) {
        if (!empty($data)) {
            $ACNTM = new Admin_Model_Content();
            $data['parent_id'] = (int) $data['category_id'];
            $parent = $ACNTM->getOne('SELECT `parent_id` FROM `categories` WHERE `id` = '.$data['parent_id']);
            if(!empty ($parent)){
                MessBox::setMessage('no_correct_parrent');
                return;
            }
            if(empty ($data['url']))
                $data['url'] = strtolower (_translit (trim($data['title'])));
            unset($data['id'], $data['category_id']);
            $ACNTM->insert('categories', $data);
        }
    }

}
